/*
New Scotland Yard is an online multiplayer adaptation
of the boardgame "Scotland Yard". Copyright (C) 2011
Massey University Software C Group 3
This program is free software: you can redistribute it
and/or modify it under the terms of the GNU General
Public License as published by the Free Software
Foundation, either version 3 of the License, or (at
your option) any later version.
This program is distributed in the hope that it will
be useful, but WITHOUT ANY WARRANTY; without even the
implied warranty of MERCHANTABILITY or FITNESS FOR A
PARTICULAR PURPOSE. See the GNU General Public
License for more details.
You should have received a copy of the GNU General
Public License along with this program. If not, see
<http://www.gnu.org/licenses/>.
*/
package nz.ac.massey.softwarec.group3.reverseAJAX;
import java.util.List;
import nz.ac.massey.softwarec.group3.game.Game;
import nz.ac.massey.softwarec.group3.session.SessionTracker;
import org.directwebremoting.ScriptSession;
import org.directwebremoting.ScriptSessionFilter;
/**
* ReverseAJAXCall - Class describing a ReverseAJAXCall.
* @version 1.0 Release
* @since 1.0
* @authors Natalie Eustace | Wanting Huang | Paul Smith | Craig Spence
*/
public class ReverseAJAXCall implements ReverseAJAXCallInterface {
private transient String page, output, element, function, params;
private transient boolean done = false;
private transient ScriptSessionFilter filter;
/**
* Getter for page.
* @return String page - The page to perform the ReverseAJAXCall on.
*/
@Override
public String getPage() {
return this.page;
}
/**
* Getter for output.
* @return String output - The output of the ReverseAJAXCall.
*/
@Override
public String getOutput() {
return this.output;
}
/**
* Getter for element.
* @return String element - The name of the HTML element on the page for the ReverseAJAXCall to edit.
*/
@Override
public String getElement() {
return this.element;
}
/**
* Getter for function.
* @return String function - The name of the function within the pages JavaScript to call.
*/
@Override
public String getFunction() {
return this.function;
}
/**
* Getter for params.
* @return String params - Any parameters required for the Javascript function which is called.
*/
@Override
public String getParams() {
return this.params;
}
/**
* Getter for done.
* @return boolean done - Whether or not the Call has been performed by the manager.
*/
@Override
public boolean isDone() {
return done;
}
/**
* Setter for done.
* @param done - set to true when the ReverseAJAXManager completed this call
*/
@Override
public void setDone(final boolean done) {
this.done = done;
}
/**
* Getter for filter.
* @return ScriptSessionFilter filter - the filter which selects what pages the call should be distributed to.
*/
@Override
public ScriptSessionFilter getFilter() {
return this.filter;
}
/**
* With a given page, element and output, send a ReverseAJAXCall to the running
* thread in ReverseAJAXManager.
* @param String page - The page on which to run the ReverseAJAXCall.
* @param String element - The element on the page in which to set the output.
* @param String output - The output to set in the element.
*/
@Override
public void createNewCall(final String page, final String element, final String output) {
this.page = page;
this.element = element;
this.output = output;
final List<ReverseAJAXCall> reverseAJAXCalls = ReverseAJAXManager.getReverseAJAXManager().getReverseAJAXCalls();
reverseAJAXCalls.add(this);
ReverseAJAXManager.getReverseAJAXManager().setReverseAJAXCalls(reverseAJAXCalls);
}
/**
* With a given page, element and output, send a ReverseAJAXCall to the running
* thread in ReverseAJAXManager, filtered by a ScriptSessionFilter.
* @param String page - The page on which to run the ReverseAJAXCall.
* @param String element - The element on the page in which to set the output.
* @param String output - The output to set in the element.
* @param String fitlerType - the type of filter that should be used.
* @param String email - the email address which is used to make the filter (Usually the person who created a game).
*/
@Override
public void createNewFilteredCall(final String page, final String element, final String output, final String filterType, final String email) {
this.page = page;
this.element = element;
this.output = output;
this.filter = createFilter(filterType, email);
final List<ReverseAJAXCall> reverseAJAXCalls = ReverseAJAXManager.getReverseAJAXManager().getReverseAJAXCalls();
reverseAJAXCalls.add(this);
ReverseAJAXManager.getReverseAJAXManager().setReverseAJAXCalls(reverseAJAXCalls);
}
/**
* With a given page, function and param, send a ReverseAJAXCall to the running
* thread in ReverseAJAXManager, filtered by a ScriptSessionFilter.
* @param String page - The page on which to run the ReverseAJAXCall.
* @param String fitlerType - the type of filter that should be used.
* @param String email - the email address which is used to make the filter (Usually the person who created a game).
* @param String function - the name of the JavaScript function to call.
* @param String params - the parameters (if any) for the Javascript function.
*/
@Override
public void createFilteredFunctionCall(final String page, final String filterType, final String email, final String function, final String params) {
this.page = page;
this.function = function;
this.params = params;
this.filter = createFilter(filterType, email);
final List<ReverseAJAXCall> reverseAJAXCalls = ReverseAJAXManager.getReverseAJAXManager().getReverseAJAXCalls();
reverseAJAXCalls.add(this);
ReverseAJAXManager.getReverseAJAXManager().setReverseAJAXCalls(reverseAJAXCalls);
}
/**
* With a given page, function and param, send a ReverseAJAXCall to the running
* thread in ReverseAJAXManager.
* @param String page - The page on which to run the ReverseAJAXCall.
* @param String function - the name of the JavaScript function to call.
* @param String params - the parameters (if any) for the Javascript function.
*/
@Override
public void createFunctionCall(final String page, final String function, final String params) {
this.page = page;
this.function = function;
this.params = params;
final List<ReverseAJAXCall> reverseAJAXCalls = ReverseAJAXManager.getReverseAJAXManager().getReverseAJAXCalls();
reverseAJAXCalls.add(this);
ReverseAJAXManager.getReverseAJAXManager().setReverseAJAXCalls(reverseAJAXCalls);
}
/**
* Create a filter of a given type, which will determine which clients are updated.
* @param String filterType - The type of filter, currently can filter to everyone in a game, or everyone in a game who is not the user who made the call.
* @param String email - The email dress of the user who made the call (Used to get the game, and to exclude that user from an "Others" filter.
* @return ScriptSessionFilter newFilter - the created filter.
*/
private ScriptSessionFilter createFilter(final String filterType, final String email) {
final Game game = SessionTracker.getSessionTracker().getGameTracker().getGameWithUserInIt(email);
ScriptSessionFilter newFilter = null;
if ("Game".equals(filterType)) {
newFilter = new GameSessionFilter(game);
} else if ("Others".equals(filterType)) {
newFilter = new OtherPlayersInGameSessionFilter(game, email);
}
return newFilter;
}
}
//ScriptSessionFilter to select users in a single game .
class GameSessionFilter implements ScriptSessionFilter
{
private final transient Game game;
public GameSessionFilter(final Game game) {
this.game = game;
}
/* (non-Javadoc)
* @see org.directwebremoting.ScriptSessionFilter#match(org.directwebremoting.ScriptSession)
*/
@Override
public boolean match(final ScriptSession session) {
final Game gameToMatch = (Game) session.getAttribute("game");
return (gameToMatch != null && gameToMatch.equals(game));
}
}
//ScriptSessionFilter to select users in a single game excluding the
//user who made the ReverseAJAXCall.
class OtherPlayersInGameSessionFilter implements ScriptSessionFilter
{
private final transient Game game;
private final transient String email;
public OtherPlayersInGameSessionFilter(final Game game, final String email) {
this.game = game;
this.email = email;
}
/* (non-Javadoc)
* @see org.directwebremoting.ScriptSessionFilter#match(org.directwebremoting.ScriptSession)
*/
@Override
public boolean match(final ScriptSession session) {
final Game gameToMatch = (Game) session.getAttribute("game");
final String emailToMatch = (String) session.getAttribute("email");
return (gameToMatch != null && gameToMatch.equals(game) && !(emailToMatch).equals(email));
}
}